﻿/*
 Author:du
 Time:2017.11.8
*/

using FutureCoreEditor;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;

public class AssetSyncTool : Editor
{
    [MenuItem("[FutureCore]/AssetSync/同步所有", false, 20)]
    private static void SyncAll()
    {
        SyncSkeleton();
        SyncEffect();
        SyncFightMap();
        SyncFrames();
    }

    [MenuItem("[FutureCore]/AssetSync/同步骨骼动画", false, 21)]
    private static void SyncSkeleton()
    {
        string formDir = Application.dataPath + "/_Game/ResourcesArt/Skeleton";
        string toDir = "Assets/_Game/Resources/Skeleton";
        FileTool.CopyPrefabsToDir(formDir, toDir);
        AssetDatabase.SaveAssets();
        AssetDatabase.Refresh();
        DebugUtil.Log("同步骨骼动画完成");
    }

    [MenuItem("[FutureCore]/AssetSync/同步特效", false, 22)]
    private static void SyncEffect()
    {
        string formDir = Application.dataPath + "/_Game/ResourcesArt/Effect";
        string toDir = "Assets/_Game/Resources/Effect";
        FileTool.CopyPrefabsToDir(formDir, toDir);
        AssetDatabase.SaveAssets();
        AssetDatabase.Refresh();
        DebugUtil.Log("同步特效完成");
    }

    [MenuItem("[FutureCore]/AssetSync/同步战斗地图", false, 23)]
    private static void SyncFightMap()
    {
        string formDir = Application.dataPath + "/_Game/Resources/Sprite/Map";
        string toDir = "Assets/_Game/Resources/Prefab/FightMap";
        FileTool.CopyPrefabsToDir(formDir, toDir);
        AssetDatabase.SaveAssets();
        AssetDatabase.Refresh();
        DebugUtil.Log("同步战斗地图完成");
    }

    [MenuItem("[FutureCore]/AssetSync/同步帧动画", false, 24)]
    private static void SyncFrames()
    {
        string formDir = Application.dataPath + "/_Game/ResourcesArt/Frames";
        string toDir = "Assets/_Game/Resources/Frames";
        FileTool.CopyPrefabsToDir(formDir, toDir);
        AssetDatabase.SaveAssets();
        AssetDatabase.Refresh();
        DebugUtil.Log("同步帧动画完成");
    }

    #region TiledMap
    private static void SyncTiledMap()
    {
        string formDir = Application.dataPath + "/_Game/ResourcesArt/Tiled2Unity/Prefabs";
        string toDir = "Assets/_Game/Resources/FightMap";
        MakeFightMapPrefab(formDir);
        FileTool.CopyPrefabsToDir(formDir, toDir);
        AssetDatabase.SaveAssets();
        AssetDatabase.Refresh();
        DebugUtil.Log("同步战斗地图完成");
    }

    private static void MakeFightMapPrefab(string formDir)
    {
        float offset = EPTiledConst.GridUnit / 2 / 100;

        DirectoryInfo formfolder = new DirectoryInfo(formDir);
        FileSystemInfo[] folders = formfolder.GetFileSystemInfos();
        foreach (FileSystemInfo info in folders)
        {
            if (!(info is DirectoryInfo) && info.Name.EndsWith(".prefab"))
            {
                string path = info.FullName.Substring(info.FullName.IndexOf("Assets"));
                GameObject prefab = AssetDatabase.LoadAssetAtPath<GameObject>(path);
                GameObject instPrefab = PrefabUtility.InstantiatePrefab(prefab) as GameObject;
                BoxCollider2D[] colliders = instPrefab.GetComponentsInChildren<BoxCollider2D>();
                if (colliders != null)
                {
                    foreach (BoxCollider2D item in colliders)
                    {
                        PrefabIconSetTool.SetIcon(item.gameObject, PrefabIconSetTool.Icon.CircleRed);
                        Vector3 temp = item.transform.localPosition;
                        item.transform.localPosition = new Vector3(temp.x + offset, temp.y - offset, temp.z);
                        DestroyImmediate(item, true);
                    }
                    Transform objectRoot = instPrefab.transform.Find("Object");
                    for (int i = 0; i < objectRoot.childCount; i++)
                    {
                        Transform pathTF = objectRoot.GetChild(i);
                        if (pathTF.name.Contains("Path"))
                        {
                            SortSubObjByNum(pathTF);
                        }
                    }

                    PrefabUtility.ReplacePrefab(instPrefab, prefab, ReplacePrefabOptions.ConnectToPrefab);
                    DestroyImmediate(instPrefab);
                    EditorUtility.SetDirty(prefab);
                }
            }
        }
    }

    private static void SortSubObjByNum(Transform pathTF)
    {
        Dictionary<int, Transform> dic = new Dictionary<int, Transform>();
        for (int i = 0; i < pathTF.childCount; i++)
        {
            Transform numObj = pathTF.GetChild(i);
            dic[numObj.name.ToInt()] = numObj;
        }
        for (int i = 1; i <= dic.Keys.Count; i++)
        {
            Transform numObj = dic[i];
            numObj.SetAsLastSibling();
        }
    }
    #endregion
}